package com.college.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BaseDao<T> extends HibernateDaoSupport{

	public T queryEntiy(final String hql,final Object... params){
		return getHibernateTemplate().execute(new HibernateCallback<T>() {
			@Override
			public T doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query=session.createQuery(hql);
				for (int i = 0; i < params.length; i++) {
					query.setParameter(i, params[i]);
				}
				List<T> list=query.list();
				if(list!=null&&list.size()>0)
				{
					return list.get(0);
				}
				return null;
			}
		});
	}
	public void save(Object object) {
		getHibernateTemplate().save(object);
	}
	
	public void update(String hql) {
		getHibernateTemplate().bulkUpdate(hql);
	}
}
